<?php
/**[栏目模型]
 * @Author: happy
 * @Email:  976123967@qq.com
 * @Date:   2015-03-14 17:11:31
 * @Last Modified by:   happy
 * @Last Modified time: 2015-03-14 17:19:29
 */
class CategoryModel extends Model
{
	protected $tableName ='category';
	private   $cache;
	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		$this->cache = S('category');
	}
	/**
	 * [get_child_list 获取分类的所有子栏目 channel标签]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_child_list($cid)
	{
		return  Data::channelList($this->cache,$cid,'&nbsp;&nbsp;&nbsp;&nbsp;');
	}


	/**
	 * [get_child_cid 子栏目的所有的cid]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_child_cid($cid)
	{
		$data =$this->get_child_list($cid);
		$result = array($cid);
		foreach($data as $v)
		{
			$result[]= $v['cid'];
		}
		return $result;
	}

		/**
	 * [get_parent_list 获取分类的所有父级栏目]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_parent_list($cid)
	{
		return Data::parentChannel($this->cache,$cid);
	}

	/**
	 * [get_parent_cid 所有父级的cid]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_parent_cid($cid)
	{
		$data =$this->get_parent_list($cid);
		$result  = array();
		foreach($data as $v)
		{
			$result[] =$v['cid'];
		}
		return $result;
	}


	/**
	 * [get_top_child 获取顶级栏目子集 树状]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_top_child($cid)
	{
		$parentCids = $this->get_parent_cid($cid);
		return Data::channelList($this->cache,$parentCids[count($parentCids)-1],'---');
	}


	/**
	 * [article_ztree 文档左侧栏目树]
	 * @return [type] [description]
	 */
	public function article_ztree()
	{
		$category = array();
		$cate = $this->cache;
		if (!empty($cate))
		{
			foreach ($cate as $n => $cat) 
			{
				$data = array();
				//过滤掉外部链接栏目
				if ($cat['cat_type'] != 3) 
				{
					//单文章栏目
					if ($cat['cat_type'] == 4) 
					{
						$link = U('Article/add',array('cid'=>$cat['cid']));
						$url = $link;
					} 
					else if ($cat['cat_type'] == 1 || $cat['cat_type'] == 2) 
					{
						$url = U('Article/index', array('cid' => $cat['cid'], 'sta' => 2));
					} 
					else 
					{
						$url = 'javascript:;';
					}
					$data['id'] = $cat['cid'];
					$data['pId'] = $cat['pid'];
					$data['url'] = $url;
					$data['target'] = 'content';
					$data['open'] = true;
					$type='';
					if($cat['cat_type']==4)
						$type='(单)';
					$data['name'] = $cat['cname'].$type;
					$category[] = $data;
				}
			}
		}
		
		return $category;
	}


	/**
	 * [find_one 查询一条记录标签typename服务]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function find_one($cid)
	{
		$data = isset($this->cache[$cid])?$this->cache[$cid]:null;
		if(!$data) return null;
		$url = $this->get_url($data);
		$data['url']  =  $url[0];
		$data['murl'] =  $url[1];
		return $data;
	}


	/**
	 * [get_url 栏目链接]
	 * @param  [type] $cat [description]
	 * @return [type]       [description]
	 */
	public function get_url($cat)
	{
		// 根据类型生成url
		$temp = array('cat_type'=>$cat['cat_type'],'cid'=>$cat['cid'],'go_url'=>$cat['go_url']);


		// 有设置跳转到子栏目 并且有子栏目
		if($cat['go_child']&& $child = $this->get_child_list($cat['cid']))
		{

			// 第一个子栏目
			$first = array_shift($child);

			$temp['cat_type']= $first['cat_type'];
			$temp['cid']= $first['cid'];
			$temp['go_url']= $first['go_url'];
		}

		$remark = strtolower($cat['remark']);
		switch ($temp['cat_type']) {
			case 1: //普通栏目
				$url  =  U('/'.$remark."_l_".$temp['cid']);
				$murl =  U(MOBILE_NAME.'/'.$cat['remark'].'/lists',$temp);
				break;
			case 2: //封面栏目
				$url =  U('/'.$remark."_c_".$temp['cid']);
				$url =  U(MOBILE_NAME.'/'.$cat['remark'].'/cover',$temp);
				break;
			case 3: //跳转
				$url = $temp['go_url'];
				break;
			case 4:
				$url  =  U('/'.$remark.'_v_'.$temp['cid']);
				$murl =  U(MOBILE_NAME.'/'.$cat['remark'].'/view',$temp);
				break;	
		}

		/*switch ($temp['cat_type']) {
			case 1: //普通栏目
				unset($temp['cat_type']);
				unset($temp['go_url']);
				$url =  U('Home/'.$cat['remark'].'/lists',$temp);
				break;
			case 2: //封面栏目
				unset($temp['cat_type']);
				$url = U('Home/'.$cat['remark'].'/cover',$temp);
				break;
			case 3: //跳转
				$url = $temp['go_url'];
				break;
			case 4:
				unset($temp['cat_type']);
				unset($temp['go_url']);
				$url = U('Home/'.$cat['remark'].'/view',$temp);
				break;	
		}*/
		return array($url,$murl);
	}

	/**
	 * [get_nav 获取导航]
	 * @param  [type] $pid [description]
	 * @return [type]      [description]
	 */
	public function get_nav($pid=0)
	{
		$nav = array();
		// 没有栏目
		if(!$this->cache) 
			return $nav;

		// 当前浏览的cid的所有父级cid
		$parentCids = array();
		if($cid = I('get.cid',null,'intval'))
			$parentCids = $this->get_parent_cid($cid);
		// 遍历栏目组合数组
	
		foreach($this->cache as $k=> $v)
		{
			$nav[$k] = $v;
			$url = $this->get_url($v);
			$nav[$k]['url']= $url[0];
			$nav[$k]['murl']= $url[1];
			// 判断高亮
			if(in_array($v['cid'], $parentCids))
				$nav[$k]['cur'] = 1;
			else
				$nav[$k]['cur'] = 0;
		
		}
		
		return  Data::channelLevel($nav,$pid);
	}
}